/*
 * @Author: 0x9DEFA478
 * @Date: 2022-02-14 22:08:03
 * @LastEditTime: 2022-08-20 20:32:23
 * @LastEditors: 0x9DEFA478
 * @Description: 
 * QQ:2652450237
 * ============================================================================================================================================
 * 
 * 
 * 
 *                                                                                               ************      ****************************
 *                                                                                             ************      ****************************  
 *                                                                                           ************      ****************************    
 *                                                                                         ************      ****************************      
 *                                                                                       ************      ************                        
 *                                                                                     ************      ************                          
 *                                                                                   ************      ************                            
 *                                                                                 ************      ************                              
 *                                                                               ************      ************                                
 *                                                                             ************      ************                                  
 *                                                                           ************      ************                                    
 *                                                                         ************      ************                                      
 *                                                                       ************      ************                                        
 *                                                                     ************      ************                                          
 *                                                                   ************      ************                                            
 *                                                                 ************      ************                                              
 *                                                               ************      ************                                                
 *                                                             ************      ************                                                  
 *                                                           ************      ************                                                    
 *                                                         ************      ************                                                      
 *                                                       ************      ************                                                        
 *                                                     ************      ************                                                          
 *                                                   ************      ************                                                            
 *                                                 ************      ************                                                              
 *                                               ************      ************                                                                
 *                                             ************      ************                                                                  
 *                                           ************      ************                                                                    
 *                                         ************      ************                                                                      
 *                                       ************      ************                                                                        
 *                                     ************      ************                                                                          
 *                                   ************      ************                                                                            
 *                                 ************      ************                                                                              
 *                               ************      ************                                                                                
 *                             ************      ************                                                                                  
 *                           ************      ************                                                                                    
 *                         ************      ************                                                                                      
 *       ****************************      ************                                                                                        
 *     ****************************      ************                                                                                          
 *   ****************************      ************                                                                                            
 * ****************************      ************                                                                                              
 * 
 * 
 * 
 * ============================================================================================================================================
 * 
 */
#include "../H_NES_Mapper.h"




extern void H_NES_Mapper7_PRG_Write(Hbyte Value,Huint16 Address,void* v);
extern Hbyte H_NES_Mapper7_PRG_Read(Huint16 Address,void* v);
extern Huint16 H_NES_Mapper7_PRG_Read16(Huint16 Address,void* v);
extern void H_NES_Mapper0_CHR_Exception_Write(Hbyte Value,Huint16 Address,void* v);
#define H_NES_Mapper7_CHR_Exception_Write H_NES_Mapper0_CHR_Exception_Write
extern void H_NES_Mapper0_CHR_Write(Hbyte Value,Huint16 Address,void* v);
#define H_NES_Mapper7_CHR_Write H_NES_Mapper0_CHR_Write
extern Hbyte H_NES_Mapper0_CHR_Read(Huint16 Address,void* v);
#define H_NES_Mapper7_CHR_Read H_NES_Mapper0_CHR_Read




#define ThisMapper Cartridge.Mapper.SpecificMapper.Mapper7




// void H_NES_Mapper7_MapperUpdate(void* v){
// }

void H_NES_Mapper7_PRG_Write_Ext(Hbyte Value,Huint16 Address,H_NES_Console* Console){
  Hbyte t_byte;

  t_byte=Console->PPU.VRAM_ScreenType;
  if(t_byte!=0x03U){
    if(((Value^Console->ThisMapper.Reg_8000)&0x10U)!=0x00U){
      if((Value&0x10U)==0U){
        switch(t_byte){
          case 0x01U:
            //命名表垂直排列
            Console->PPU.NameTable[0]=&((Hbyte*)Console->PPU.VRAM)[0];
            Console->PPU.NameTable[1]=&((Hbyte*)Console->PPU.VRAM)[0];
            Console->PPU.NameTable[2]=&((Hbyte*)Console->PPU.VRAM)[0x400];
            Console->PPU.NameTable[3]=&((Hbyte*)Console->PPU.VRAM)[0x400];
            Console->PPU.VRAM_ScreenType=t_byte;
            break;
          case 0x02U:
            //命名表水平排列
            Console->PPU.NameTable[0]=&((Hbyte*)Console->PPU.VRAM)[0];
            Console->PPU.NameTable[1]=&((Hbyte*)Console->PPU.VRAM)[0x400];
            Console->PPU.NameTable[2]=&((Hbyte*)Console->PPU.VRAM)[0];
            Console->PPU.NameTable[3]=&((Hbyte*)Console->PPU.VRAM)[0x400];
            Console->PPU.VRAM_ScreenType=t_byte;
            break;
          default:
            break;
        }
      }else{
        Console->ThisMapper.VRAM_ScreenType=t_byte;
        Console->PPU.NameTable[0]=&((Hbyte*)Console->PPU.VRAM)[0];
        Console->PPU.NameTable[1]=&((Hbyte*)Console->PPU.VRAM)[0];
        Console->PPU.NameTable[2]=&((Hbyte*)Console->PPU.VRAM)[0];
        Console->PPU.NameTable[3]=&((Hbyte*)Console->PPU.VRAM)[0];
        Console->PPU.VRAM_ScreenType=0x00U;
      }
    }
  }

  Console->ThisMapper.Reg_8000=Value;
  Console->ThisMapper.Base8000=&Console->Cartridge.Mapper.PRG_ROM[(((Huint32)Value&0x0FU)<<15)%Console->Cartridge.Mapper.PRG_ROM_Size];
}




static void GetProgress(void* v,void* Progress){
  H_NES_Console* Console;
  Hbyte* p;
  
  Console=v;
  p=Progress;

  H_NES_Mapper_DefaultGetProgress(Console,p);

  p=&p[H_NES_Mapper_DefaultProgressSize(Console)];

  p[0]=Console->ThisMapper.Reg_8000;
  p[1]=Console->ThisMapper.VRAM_ScreenType;

}

static void LoadProgress(void* v,void* Progress){
  H_NES_Console* Console;
  Hbyte* p;
  
  Console=v;
  p=Progress;

  H_NES_Mapper_DefaultLoadProgress(Console,p);

  p=&p[H_NES_Mapper_DefaultProgressSize(Console)];

  Console->ThisMapper.Reg_8000=p[0];
  Console->ThisMapper.VRAM_ScreenType=p[1];
  
  Console->ThisMapper.Base8000=&Console->Cartridge.Mapper.PRG_ROM[(((Huint32)Console->ThisMapper.Reg_8000&0x0FU)<<15)%Console->Cartridge.Mapper.PRG_ROM_Size];

}

int H_NES_Mapper7_Init(void* v){
  H_NES_Console* Console;

  Console=v;


  if(Console->Cartridge.Mapper.PRG_RAM_Size<0){
    Console->Cartridge.Mapper.PRG_RAM_Size=0x2000;
    Console->Cartridge.Mapper.PRG_RAM=Console->Memory.Malloc(0x2000);
    if(Console->Cartridge.Mapper.PRG_RAM==NULL){
      return vH_NES_Result_PRG_RAM_AllocFail;
    }
  }


  Console->ThisMapper.Base8000=&Console->Cartridge.Mapper.PRG_ROM[Console->Cartridge.Mapper.PRG_ROM_Size-0x8000U];


  Console->Cartridge.Mapper.DeInit=NULL;

  
  Console->Cartridge.PRG_Write=H_NES_Mapper7_PRG_Write;
  Console->Cartridge.PRG_Read=H_NES_Mapper7_PRG_Read;
  Console->Cartridge.PRG_Read16=H_NES_Mapper7_PRG_Read16;
  
  if(Console->Cartridge.Mapper.CHR_ROM_Size==0U){
    Console->Cartridge.CHR_Write=H_NES_Mapper7_CHR_Write;
  }else{
    Console->Cartridge.CHR_Write=H_NES_Mapper7_CHR_Exception_Write;
  }
  Console->Cartridge.CHR_Read=H_NES_Mapper7_CHR_Read;

  Console->Cartridge.Progress.Size=2+H_NES_Mapper_DefaultProgressSize(Console);
  Console->Cartridge.Progress.Get=GetProgress;
  Console->Cartridge.Progress.Load=LoadProgress;
  
  

  return vH_NES_Result_OK;
}




#undef ThisMapper



